/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.mall.util.jsp.remote.EvaluateJspServiceImpl
   Module Description   :

   Date Created      : 2010/1/7
   Original Author   : andy.chuang
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.mall.util.jsp.remote;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Date;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.mall.util.jsp.JspCacheManager;

/**
 * @author andy.chuang
 *
 */
public class RemoteEvaluateJspServiceImpl implements RemoteEvaluateJspService{
    @Resource
    private JspCacheManager jspCacheManager;
    @Resource
    private BaseManager baseMgr;
    private Logger LOGGER=LoggerFactory.getLogger(RemoteEvaluateJspServiceImpl.class);
    public String evaluateBlock(String cacheKey, String scheme, String serverName, int servicePort, String contextPath, String url, String appendParameter){
	String content=null;
	String message="true";
	String fullUrl=scheme+"://"+serverName+":"+servicePort+contextPath+url;
	BufferedReader br =null;
	    try {
		
		
			
		URL oURL = new URL(new String(fullUrl));
		
		br= new BufferedReader(new
		InputStreamReader(oURL.openStream(),"UTF-8"));
		String line="";
		StringBuffer requestString=new StringBuffer("");
		while ((line = br.readLine()) != null)
		requestString.append(line);
		content=requestString.toString();
		
	    } catch (Exception e) {
		LOGGER.warn(" load url failed!! url:"+fullUrl);
		e.printStackTrace();
		message=e.getMessage();
	    }finally{
		if(br!=null)
		    try {
			br.close();
		    } catch (IOException e) {
			LOGGER.warn("close bufferedReader failed!! url:"+fullUrl);
			e.printStackTrace();
		    }
	    }
		
	    jspCacheManager.addCache(jspCacheManager.getKey(cacheKey, url, appendParameter, null), content);
	    return message;
    }
    public String evaluatePage(String fullUrl){
	BufferedReader br =null;
	String message="true";
	 try {
		URL oURL = new URL(new String(fullUrl));
		
		br= new BufferedReader(new
		InputStreamReader(oURL.openStream(),"UTF-8"));
		String line="";
		StringBuffer requestString=new StringBuffer("");
		while ((line = br.readLine()) != null)
		requestString.append(line);
		
		br.close();
	    } catch (Exception e) {
		LOGGER.warn(" load url failed!! url:"+fullUrl);
		e.printStackTrace();
		message=e.getMessage();
	    }finally{
		if(br!=null)
		    try {
			br.close();
		    } catch (IOException e) {
			LOGGER.warn("close bufferedReader failed!! url:"+fullUrl);
			e.printStackTrace();
		    }
	    }
	    return message;
    }
    /* (non-Javadoc)
     * @see com.yaodian100.mall.util.jsp.remote.RemoteEvaluateJspService#evaluateProduct(java.lang.Integer)
     */
    public String evaluateProduct(Integer count) {
	String message="true";
	Date currentDate=new Date();
	String hql="select p.productId from Product where modifyDate<=?";
	Object[] values=new Object[]{currentDate};
	Object[] results=null;
	if(count!=null){
	    results=baseMgr.customHqlQuery(hql, values, 0, count);
	}else{
	    results=baseMgr.customHqlQuery(hql, values);
	}
	for(Object obj:results){
	    String productId=(String)obj;
	    String url="http://localhost/ecmall/product/product.do?productId="+productId;
	    evaluatePage(url);
	}
	return message;
    }
}
